import json
import os


class File:
    def read_folder(self, directory):
        txt_files = []
        # 遍历指定目录及其所有子目录
        for root, dirs, files in os.walk(directory):
            for file in files:
                # 检查文件扩展名是否为.txt
                if file.endswith(".txt"):
                    if "英文" not in file:
                        if "-" in file:
                            # 获取文件的完整路径
                            file_path = os.path.join(root, file)
                            file = file.split(".")[0].replace(" ", "").replace("：", "")
                            year = os.path.basename(os.path.dirname(file_path))
                            # 将文件名和路径作为元组添加到列表中
                            txt_files.append((file, year, file_path))
        return txt_files

    def save_json(self, file_path, data):
        # 打开一个文件，准备写入JSON数据
        with open(file_path, "w", encoding="utf-8") as file:
            # 使用json.dump()方法将字典转换为JSON格式的字符串，并写入文件
            json.dump(data, file, ensure_ascii=False, indent=4)

    def search_text(self, file_path):
        keywords = [
            "人工智能应用",
            "人工智能",
            "AI",
            "AI产品",
            "AI智能",
            "AI人工智能",
            "AI芯片人工智能技术",
            "人工智能学习",
            "人机协同",
            "知识图谱",
            "增强现实",
            "虚拟现实",
            "智能教育",
            "智能政务",
            "商业智能",
            "智能养老",
            "智能客服",
            "智慧银行",
            "智慧金融",
            "大数据营销",
            "智能保险",
            "智能零售",
            "智能医疗",
            "智能运输",
            "智能家居",
            "大数据风控",
            "可穿戴产品",
            "大数据平台",
            "增强智能",
            "大数据运营",
            "智能农业",
            "智能音箱",
            "大数据分析",
            "大数据管理",
            "智能计算",
            "智能监管",
            "智能投顾",
            "智能语音",
            "智能体",
            "大数据处理",
            "分布式计算",
            "智能传感器",
            "智能搜索",
            "智能环保",
            "数字技术",
            "自动控制",
            "智能控制",
            "智能制造",
            "智能网联",
            "无人零售",
            "智能穿戴",
            "智能交通",
            "智能营销",
            "智能物流",
            "智能仓储",
            "智能生产",
            "智能管理",
            "人工智能应用",
            "机器智能",
            "脑机交互",
            "情感交互",
            "体感交互",
            "CIMS",
            "人证核验",
            "AlOps",
            "AI/ML推理",
            "人的智能",
            "人类智能",
            "人类能力",
            "自然语言处理",
            "AI写作",
            "BERT",
            "聊天机器人",
            "语言模型",
            "机器翻译",
            "问答系统",
            "语义理解",
            "自然语言生成",
            "自然语言问答",
            "人机交互",
            "人机对话",
            "语义搜索",
            "语音识别",
            "语音合成",
            "语音交互",
            "语音测评",
            "声纹识别",
            "机器学习",
            "监督学习",
            "无监督学习",
            "强化学习",
            "迁徙学习",
            "主动学习",
            "演化学习",
            "决策树",
            "深度置信",
            "粒子群优化",
            "多目标演化",
            "价值挖掘",
            "个性化推荐",
            "随机森林算法",
            "AdaBoost",
            "模式识别",
            "数据挖掘",
            "特征提取",
            "神经网络",
            "深度学习",
            "深度神经网络",
            "卷积神经网络",
            "循环神经网络",
            "长短期记忆",
            "LSTM",
            "受限玻尔兹曼",
            "MXNet",
            "自主驾驶",
            "自动驾驶",
            "无人驾驶",
            "高级驾驶辅助系统",
            "自动驾驶系统",
            "生成式人工智能",
            "ChatGPT",
            "生成对抗网络",
            "数字人",
            "AIGC",
            "智能创作",
            "文本生成",
            "图像生成",
            "多模态生成",
            "大模型",
            "扩散模型",
            "AI生成内容",
            "预训练大模型",
            "视觉图像识别",
            "计算机视觉",
            "图像识别",
            "机器视觉",
            "图像理解",
            "三维人工智能视觉",
            "动态视觉",
            "视频编解码",
            "人脸识别",
            "虹膜识别",
            "指静脉识别",
            "生物特征识别",
            "步态识别",
            "特征识别",
            "机器人技术",
            "医疗机器人",
            "陪伴机器人",
            "工业机器人",
            "智能机器人",
            "机器人流程自动化",
        ]

        content = ""
        with open(file_path, "r", encoding="utf-8") as file:
            # 读取整个文件内容到变量content中
            for segment in file:
                if any(keyword in segment for keyword in keywords):
                    content = (
                        content + segment.strip()
                    )  # 使用strip()移除每行末尾的换行符
        if len(content) < 1:
            content = "公司没有使用任何高新技术。"
        print(len(content))
        print(content)
        return content

    def read_json(self, json_path):
        with open(json_path, "r", encoding="utf-8") as file:
            try:
                data = json.load(file)
            except json.JSONDecodeError as e:
                print(f"JSONDecodeError: {e.msg}")
                data = None
        return data
